package com.tanhua.dubbo.api;

import com.tanhua.domain.db.User;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long loveUserId) {
        UserLike userLike = new UserLike();
        userLike.setId(new ObjectId());
        userLike.setUserId(userId);
        userLike.setLikeUserId(loveUserId);
        userLike.setCreated(new Date().getTime());
        mongoTemplate.save(userLike);
    }

    @Override
    public Boolean eachLove(Long userId, Long loveUserId) {
//        查询两次

        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(loveUserId));
        boolean exists = mongoTemplate.exists(query, UserLike.class);

        Query query1 = new Query(Criteria.where("userId").is(loveUserId).and("likeUserId").is(  userId));
        boolean exists1 = mongoTemplate.exists(query1, UserLike.class);


        return exists&&exists1;
    }

    @Override
    public void delete(Long userId, Long loveUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(loveUserId));
        mongoTemplate.remove(query, UserLike.class);

    }
}
